package com.cms.task;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import net.vinote.smart.platform.comm.DateFunction;
import net.vinote.smart.platform.timer.AbstractSmartTimerTask;

import org.json.JSONObject;
import org.jsoup.Connection;
import org.jsoup.helper.HttpConnection;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * 工行黄金价格采集
 * 
 * @author Seer
 * @version IcbcGoldMiningTask.java, v 0.1 2015年3月5日 下午7:15:31 Seer Exp.
 */
public class IcbcGoldMiningTask extends AbstractSmartTimerTask {
	private File f = new File(
			"G:\\Smart\\smart-platform\\data.txt");
	FileOutputStream fos = null;

	@Override
	public void run() {
		try {
			if (fos == null) {
				fos = new FileOutputStream(f);
			}
			Connection connection = HttpConnection
					.connect("http://www.icbc.com.cn/ICBCDynamicSite/Charts/GoldTendencyPicture.aspx");
			Elements els = connection.get().getElementById("TABLE1")
					.select("tr").get(1).select("td");
			Element in = els.get(2);
			Element out = els.get(3);
			JSONObject json = new JSONObject();
			json.put("time", DateFunction.getCurrentTime());
			json.put("in", in.text());
			json.put("out", out.text());
			fos.write(json.toString().getBytes());
			fos.write("\r\n".getBytes());
			fos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
